<?php 

function render_cell($level , $cell, $number_length)  {
/******************GLOBAL VARIABLES*************************/
	
	$s_d_exception=0;

	$n2c[0][0]="";
	$n2c[0][1]="un";
	$n2c[0][2]="doi";
	$n2c[0][3]="trei";
	$n2c[0][4]="patru";
	$n2c[0][5]="cinci";
	$n2c[0][6]="sase";
	$n2c[0][7]="sapte";
	$n2c[0][8]="opt";
	$n2c[0][9]="noua";
	
	
	$n2c['hundreds']="sute";
	$n2c['onehundred']="osuta";
	$n2c['twohundreds']="douasute";
	//  Decimals array container 
	
	$n2c[1][0]="";
	$n2c[1][1]="zece";
	$n2c[1][2]="douazeci";
	$n2c[1][3]="treizeci";
	$n2c[1][4]="patruzeci";
	$n2c[1][5]="cincizeci";
	$n2c[1][6]="saizeci";
	$n2c[1][7]="saptezeci";
	$n2c[1][8]="optzeci";
	$n2c[1][9]="nouazeci";
	
	$n2c['state_one_f']="o";
	$n2c['state_one_m']="un";
	$n2c['state_two']="doua";
	$n2c['concat']="si";
	
	//  Multiplieres array container
	
	$n2c['multiple'][0]="";
	$n2c['multiple'][1]="mii";
	$n2c['multiple'][2]="milioane";
	$n2c['multiple'][3]="miliarde";
	$n2c['multiple'][4]="biliarde";
	$n2c['multiple'][5]="triliarde";
	
	// Single multiples
	$n2c['smultiple'][0]="";
	$n2c['smultiple'][1]="mie";
	$n2c['smultiple'][2]="milion";
	$n2c['smultiple'][3]="miliard";
	$n2c['smultiple'][4]="biliard";
	$n2c['smultiple'][5]="triliard";
	
	//  Exceptions array container
	
	$n2c['exec'][0]="zece";
	$n2c['exec'][1]="unsprezece";
	$n2c['exec'][2]="doisprezece";
	$n2c['exec'][3]="treisprezece";
	$n2c['exec'][4]="patrusprezece";
	$n2c['exec'][5]="cincisprezece";
	$n2c['exec'][6]="saisprezece";
	$n2c['exec'][7]="saptesprezece";
	$n2c['exec'][8]="optsprezece";
	$n2c['exec'][9]="nouasprezece";	
	
	// Initiate response string
	$response="";
	
	// Set level to corelate to config file
	$level=$number_length - $level-1;

	// Hack the planet
	// If cell containes less than 3 numbers shift to right
	if(count($cell)==1)  {		
		$cell[2]=$cell[0];
		unset($cell[0]);
		unset($cell[1]);
		
	}elseif(count($cell)==2) {
		$cell[2]=$cell[1];
		$cell[1]=$cell[0];
		unset($cell[0]);
	}else{}	

			// If the first poisition in the cell is set and not null...
			if(isset($cell[0]) && $cell[0]!=0)  {
				
				// Handle exceptions first
				if($cell[0]==1)  {
					
					// Add exception for one
					$response.=$n2c['onehundred'];
					
				}elseif($cell[0]==2) {
					
					// Add exception for 2
					$response.=$n2c['twohundreds'];
					
				}else  {
					
					// Add character translation from config file
					$response.=$n2c[0][$cell[0]].$n2c['hundreds'];
				}				
			}elseif(isset($cell[0]) && $cell[0]==0){ 
			// If the first position in the cell is set and null				
			
			}else{}

			 		
			if(isset($cell[1]) && $cell[1]!=0)  {
				
				// Handle exceptions first
				
				if($cell[1]==1)  {
					$response.=$n2c['exec'][$cell[2]];
				}else {
					// If there is content for units and is not null add "si" string
					if($cell[2]!=0)  {
						$response.=$n2c[1][$cell[1]].$n2c['concat'];			
					}else{
						$response.=$n2c[1][$cell[1]];			
					}					
				}				
			}else{}

			if(isset($cell[2]) && $cell[2]!=0)  {
				// Handle exceptions first

				if(isset($cell[1]) && ($cell[1]==1))  {
					// Do not append content
					// This is done at the previous level and handles exceptions
				}elseif((isset($cell[1]) && $cell[1]==0) ||  $cell[2]!=0) {									

					//	If the $level==1
					//  Add exception to unit	
						
					if($level==0)  {
						if($cell[2]==1){														
							$response.=$n2c['state_one_m'];		
							$response.=$n2c['smultiple'][$level];
							$s_d_exception=1;
						}else{
							$response.=$n2c[0][$cell[2]];		
						}						
					}elseif($level==1)  {
						if($cell[2]==1){														
							$response.=$n2c['state_one_f'];		
							$response.=$n2c['smultiple'][$level];
							$s_d_exception=1;
						}elseif($cell[2]==2){
							$response.=$n2c['state_two'];		
						}else{
							$response.=$n2c[0][$cell[2]];		
						}
					}elseif($level>1){

						// If the level differs from 1 
						if($cell[2]==1){														
							$response.=$n2c[0][$cell[2]];		
							$response.=$n2c['multiple'][$level];
							$s_d_exception=1;
						}elseif($cell[2]==2){
							$response.=$n2c['state_two'];		
						}else{
							$response.=$n2c[0][$cell[2]];		
						}												
					}
					
				}else{
					$response.=$n2c[0][$cell[2]];
				}
		     // If the single/double exception is not set append normal level
			 // Append multiplier level
			 if($s_d_exception==0){
			 	$response.=$n2c['multiple'][$level];	
			 }
			 
		    				

			// If the unit is null 	
			}elseif($cell[1]==0 && $cell[0]==0) {
				// If all the cell units are null								
			}else{
				$response.=$n2c['multiple'][$level];
			}	
				
	//$response=implode($cell).$n2c['multiple'][$level]."<br />";
		
	//
	return $response;
	
	//return implode($cell);
}


/*
*  Function for rendering the content of the input number to cells
*
*  @String argument
*/


function render_number($init_float)  {

	$number_length=0;
	$response="";

	if(!isset($init_float)   ||   $init_float!="")      {		
		//  Split the incoming number as a string to an array
		
		$input_float=explode( "," , $init_float);		
			
		// Grab the input string
		$input_str=str_split($input_float[0]);

		// Reverse the array
		 $input_str=array_reverse($input_str);
		
		// Echo the number
		//echo $init_float."<br /><br /><br />";
		//die();
		
		
		// Calculate the length of the input number
		$k=count($input_str);
		$number_length=($k - $k%3 ) / 3 ;
		if($k%3>0)   {
			// If modulus is not null add one unit to the length....
			// adica da cu rest si imi trebuie parte intreaga plus 1
			$number_length++;
		}		
		
	    //  Chunk the array in max 3 elements arrays 		
		$arr=array_chunk($input_str , 3 , TRUE);
		
		// Reverse the array -> flips the order for the 3 elements arrayz as elements of an array
		$arr=array_reverse($arr);
		for($i=0 ;  $i<count($arr) ; $i++)  {
			// For each cell , array of 3 elements , run render process from integer to character
			$response .=render_cell($i , array_reverse($arr[$i]), $number_length);		
		}
	}
	return $response;
}	

?>